

*****************************************************************
*																*
*				Title: simulate_model.do.        	 	 	   	*		
*															    *
*****************************************************************

/*  ================================  NOTES  ================================

+ PURPSOSE:
	
	In this do-file we simulate the model and produce all tables/figues
	based on the model simulation
	
 
		
+ Inputs: none

	


==============================  TOP MATTER ==============================*/

clear all

*set paths 
	global output_main "`pdir'/outputs/main_paper"
	global output_appendix "`pdir'/outputs/appendix"




set obs 1000  // this is also the number of firms in the simulation


***   Set seed  ************************

set seed 374076

***   Set parameters   ****
global eps = 3 //elasticity of substitution in consumption
global M = 5 // number of production-labor tasks
global f = 1 // fixed cost
global A = 1 // overall productivity level
global beta = 0.6 // discount rate over a 10-year period

***************************

***   Set a few given variables   ****
global R = 10000   //total revenues in the cotton market
global w = 1 // wage rate
gen min_draw = 0  //lower bound for gamma draws
gen max_draw = 1  //upper bound for gamma draws

**************************************



*quietly{
	
*Individual task-specific productivity draws
foreach i of numlist 1(1)$M {
	gen gamma_`i' = runiform(min_draw,max_draw)
}

foreach i of numlist 1(1)$M {
	gen gamma_`i'_inv = 1/gamma_`i'
}
egen tot_gamma_inv = rowtotal(gamma_*_inv)

*Firm productivity
gen phi = $A/( (1/$M)*tot_gamma_inv )
sort phi
drop gamma_*_inv tot_gamma_inv
gen firm_number = _n

*Output prices
gen p = ($eps / ( $eps - 1) ) * ( $w / phi)
gen p_eps = p^(1-$eps) 


/////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// Initial Period ////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////

**Loop to find which firms are surviving: start with all firms surviving, then compute profits. If last firm makes negative profits, exclude it and recalculate the Price Index P...
gen surviving = 1
local z = 1
quietly{
while `z' <=1000 {
*Price Index
egen p_eps_sum = sum(p_eps) if firm_number >= `z'
gen P = p_eps_sum^(1/(1-$eps)) if firm_number >= `z'  
drop p_eps_sum

*Firm profits:
gen Pi = (1/$eps)*$R*( (($eps-1)/$eps) * P *phi/$w )^($eps-1) - $w*$f
noi: display "Profits of lowest-prd firm " `z' ":" Pi[`z']
	if Pi[`z']<0 {
		
		replace surviving = 0 in `z'
		local z = `z'+1
		drop P Pi
	}
	else {
	local z = 1001
	noi: display(`z')
	rename surviving surviving_t1
	rename p p_t1
	rename P P_t1
	rename Pi Pi_t1
	}
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// Innovation Period /////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////


***Among all surviving firms, compute the cutoff above which firms produce in period 2, while those below the cutoff search for better org. knowledge (i.e., innovate)	
quietly{	
*Percentage of surviving firms in initial period (i.e., firms that have to decide whether to actually produce or innovate)
egen tot_surviving_t1 = sum(surviving_t1)
gen exit_t1 = _N - tot_surviving_t1
gen perc_operating_t1 = tot_surviving_t1/_N
noi: display "Percentage of surviving firms:" perc_operating_t1[_N]	


**** Now look into the decision to innovate (search for better operational knowledge) vs. produce in the first period:
gen exp_life_Pi_search = .  //expected profits over remaining firm life time (periods 2 and 3) if searching in period 2
gen exp_life_Pi_produce = . //expected profits over remaining firm life time (periods 2 and 3) if producing in period 2
gen search = 0 if surviving_t1 == 1 //indicator for the search variable

gen produce = surviving_t1 // firms that produce rather than innovate this year (initial value: all surviving firms)
local x = exit_t1[_N] + 1 //starting point for the lowest-productivity firm that is producing (this is the lowest-prd firm that survived the initial period, and in the initial guess we assume all surviving firms produce in period 2)
replace produce = 0 in `x' //start loop with at least one survivign firm deciding to search (i.e., the lowest-prd surviving firm)
**Loop to find cutoff m for lowest-prd firm that will search in period 2:
while `x' <=1000 {
noi: display "Lowest-productivity firm that is producing (rather than searching): " `x'

***** This period's profits if firms up to cutoff x search and all above x produce:	
	*First compute this period's price index, depending on how many firms are searching vs. producing: 
		*Output prices in the innovation period (we use the subscript "now" since this is the period when the decision to innovate is made):
		gen p_now = ($eps / ( $eps - 1) ) * ( $w / phi)	if surviving_t1==1 & produce == 1 //this period's prices for firms that survived and decide to produce rather than search
		gen p_eps_now = p_now^(1-$eps)
		*Compute corresponding price index:
		egen p_eps_sum_now = sum(p_eps_now)  if surviving_t1==1
		gen P_now = p_eps_sum_now^(1/(1-$eps)) //note that this is computed only across producing firms, excluding the cutoff firm x. Thus, x takes the Price Index 
												// from all other producing firms as given when itself decides whether to search or produce	
		drop p_eps_sum_now
	*Now compute profits in the innovation period:
		gen Pi_now = (1/$eps)*$R*( (($eps-1)/$eps) * P_now *phi/$w )^($eps-1) - $w*$f
		drop p_now p_eps_now P_now

**Next period's profits (in the final period) if firms up to cutoff x search in the innovation period, upgrading their productivity, while all above x produce in the innovation period and keep their initial productity
	*Expected new productivity draw if all firms above cutoff x continue to produce (rather than search for better operating knowledge):
	egen exp_phi_draw1 = mean(phi) if produce == 1
	gen exp_phi_draw = exp_phi_draw1[_N] if surviving_t1 == 1 // otherwise the mean is not entered for firm x
		drop exp_phi_draw1
	gen phi_next = max(exp_phi_draw, phi) if surviving_t1==1 & produce == 0 // next (final) period's expected productivity for firms that operate in the first period and decide to innovate (search)
	replace phi_next =  phi if surviving_t1==1 & produce == 1 // next period's expected productivity for firms that operate and decide to produce rather than search
	*Output prices next (final) period:
	gen p_next = ($eps / ( $eps - 1) ) * ( $w / phi_next) if firm_number~=`x' //exclude the cutoff firm itself from the price index
	gen p_eps_next = p_next^(1-$eps) 
	**Check: Make sure that all surviving firms (those that searched, and those that produced in period 2) should be expected to operate in the final period:
		gen operating_next = surviving_t1 // start with lowest-prd firm next period (i.e., the one just above cutoff x that decided to produce in the current period rather than innovating)
		sort phi_next  //need to sort again by productivity because now the firm at cutoff x has upgraded its productivity
		local c = 1 // start with lowest-prd firm next period (i.e., the one just above cutoff x that decided to produce in the current period rather than innovating)
		while `c' <=1000 {
			*Compute corresponding price index:
			egen p_eps_sum_next = sum(p_eps_next) if operating_next == 1 
			gen P_next = p_eps_sum_next^(1/(1-$eps)) if operating_next == 1 	
			drop p_eps_sum_next
			*Expected firm profits next period:
			gen Pi_next = (1/$eps)*$R*( (($eps-1)/$eps) * P_next *phi_next/$w )^($eps-1) - $w*$f
			
			if Pi_next[`c']<0 {
				noi: display "Operate check t2 not o.k." 
				replace operating_next = 0 in `c'
				local c = `c'+1
				drop P_next Pi_next
				}
				else {
				local c = 1001 //forces the loop to end
				}
		}
	
	sort phi // now sort again according to original productivity draws
	
	*Finally, compute expected profits next period if firm x is not innovating: (note that these are the same for all firms other than x, since x was excluded when computing the Price Index)
	replace phi_next = phi if firm_number==`x'
	gen P_next_x_prod = (1/$eps)*$R*( (($eps-1)/$eps) * P_next *phi_next/$w )^($eps-1) - $w*$f
	drop exp_phi_draw phi_next p_next p_eps_next operating_next P_next

	
	*Compute profits of cutoff firm x if NOT producing (i.e., searching) in the innovation period (with all firms with produce==1 producing this period): 
	replace exp_life_Pi_search = $beta * Pi_next in `x' 

	*Compute profits of cutoff firm x if producing in the innovation period (with all firms with produce==1 producing): 
	replace exp_life_Pi_produce = Pi_now + $beta * P_next_x_prod in `x' 
	
	*Code the "search" and "produce" indicators	
	if exp_life_Pi_search[`x']>exp_life_Pi_produce[`x'] { //for firm x, searching is more profitable than producing in period 2
		replace search = 1 in `x'
		local x = `x'+1
		replace produce = 0 in `x' //update, so that one less firm is producing, since the previous (just lower-ranked) firm searches
		
		drop Pi_now Pi_next P_next_x_prod
		}
		else {
			replace produce = 1 in `x'
			replace search = 0 in `x'
			local x = 1001 //break the loop
			gen Pi_t2_prd = Pi_now if produce==1 // profits of firms that are producing (rather than searching) in period 2
			drop Pi_now P_next_x_prod Pi_next
		}

}	
}

**** Assign the new productivity draw (rather than the expected one) to searching firms:
quietly{
gen phi_t3 = phi // productivity in period 3 (the final period); to be updated for searching firms in the lines that follow
egen tot_producing_t2 = sum(produce)  //number of producing firms in period 2
local L_bound = _N-tot_producing_t2[1] + 1 //lower bound for the random draw of firm numbers -- this is the lowest-prd firm that is producing in period 2 and can thus provide a match to a searching firm
dis `L_bound'
gen firm_match =  runiformint(`L_bound', _N) if search==1 //each searching firm is matched at random to a producing firm

	*Now assign productivity from the matched firm:
	local s = exit_t1[_N] + 1 // lowest-prd firm that is surviving (and searching) in period 1
	dis `s'
	while `s' < `L_bound' { //note that `L_bound' is also the first (lowest-prd) firm that is producing (not searching); so we need to assign matches up to `L_bound'-1
		local c = firm_match[`s']
		dis `c'
		replace phi_t3 = phi[`c'] in `s'
		local s=`s'+1
	}
drop tot_producing_t2 firm_match
}


***Firm profits and productivity in period 3
quietly{

*Output prices
gen p_t3 = ($eps / ( $eps - 1) ) * ( $w / phi_t3) if surviving_t1==1 
gen p_eps_t3 = p_t3^(1-$eps)  if surviving_t1==1 

	**Check if all firms in period 3 indeed make positive profits (those that searched, and those that produced in period 2):
	gen surviving_t3 = 1  if surviving_t1==1 //start by assuming that all firms survive
	local z = exit_t1[_N] + 1 //starting point: The lowest-prd firm that survived period 1
	quietly{
	while `z' <=1000 {
	*Price Index
	egen p_eps_sum_t3 = sum(p_eps_t3) if firm_number >= `z'
	gen P_t3 = p_eps_sum_t3^(1/(1-$eps)) if firm_number >= `z'  
	drop p_eps_sum_t3

	*Firm profits:
	gen Pi_t3 = (1/$eps)*$R*( (($eps-1)/$eps) * P_t3 *phi_t3/$w )^($eps-1) - $w*$f
		if Pi_t3[`z']<0 {
			noi: display "Operate check t3 not o.k."
			replace surviving_t3 = 0 in `z'
			local z = `z'+1
		}
		else {
		local z = 1001 // break the loop
		}
	}
	}	
drop p_eps_t3	
}


*Compute variables
quietly{
rename phi phi_t1
*Firm output and revenues:
gen q_t1 = (1/p_t1)^$eps * P_t1^($eps-1) * $R if surviving_t1==1
gen r_t1 = (P_t1/p_t1)^($eps-1) * $R if surviving_t1==1
gen q_t3 = (1/p_t3)^$eps * P_t3^($eps-1) * $R if surviving_t1==1
gen r_t3 = (P_t3/p_t3)^($eps-1) * $R if surviving_t1==1


*Firm labor
gen l_t1 = $f + q_t1/phi_t1  if surviving_t1==1
gen l_t3 = $f + q_t3/phi_t3  if surviving_t1==1

*Labor productivity:
gen lny_ov_l_operate_t1 = ln(r_t1/l_t1) if surviving_t1==1
gen lny_ov_l_operate_t3 = ln(r_t3/l_t3) if surviving_t1==1

*Labor productivity (using quantities):
gen lnq_ov_l_operate_t1 = ln(q_t1/l_t1) if surviving_t1==1
gen lnq_ov_l_operate_t3 = ln(q_t3/l_t3) if surviving_t1==1

*Productivity draws= for all firms in period 1
gen ln_phi_t1_all = ln(phi_t1)

*Just the productivity draws (conditional on operating)
gen ln_phi_t1 = ln(phi_t1) if surviving_t1==1
gen ln_phi_t3 = ln(phi_t3) if surviving_t1==1
}


*********** Plot productivity (phi) in the two time periods



**Figure A.15 (left panel): Entrepreneur's decision to operate
local bandwidth = 0.2
sum ln_phi_t1_all, d
local 5pctile = r(p5) //needed so we ignore the bottom 5% draws in the plot
sum ln_phi_t1_all if surviving_t1==1
local o = r(min) //this is productivity of the lowest-prd firm that decides to operate in period 1
twoway (kdensity ln_phi_t1_all if ln_phi_t1_all>`5pctile', bwidth(`bandwidth') lcolor(black) lpattern(dash) lwidth(thick)), xline(`o', lcolor(green) lwidth(medthick)) ///
xscale(r(-3 -0.2))  xlabel(none) yscale(r(0 1.25))  ylabel(0(0.2)1.2) xtitle("ln(productivity)", size(medlarge))  ytitle("Density", size(medlarge))  ///
legend(order(1 "Initial Period" 2 "Later Period") size(medium) position(11) ring(0) rows(2)) scheme(s1color) ///
text(0.05 `o' "cutoff for" "surviving", place(e) color(green) size(medium)) ///
text(0.8 -2.8 "entrepreneur exits", place(e) color(red) size(medlarge)) ///
text(0.8 -1.4 "survive initial period", place(e) color(red) size(medlarge)) 
graph export "$output_appendix/Figures/Figure_A15A.eps", as(eps) replace
graph export "$output_appendix/Figures/Figure_A15A.pdf", as(pdf) replace


**Figure A.15 (right panel): Entrepreneur's decision to search vs. produce
local bandwidth = 0.2
sum ln_phi_t1_all if surviving_t1==1
local o = r(min) //this is productivity of the lowest-prd firm that decides to operate in period 1
sum ln_phi_t1 if produce==1
local m = r(min) //this is productivity of the lowest-prd firm that decides to produce in period 1
twoway (kdensity ln_phi_t1, bwidth(`bandwidth') lcolor(black) lpattern(dash) lwidth(thick)), xline(`o', lcolor(green) lwidth(medthick)) xline(`m', lcolor(navy) lwidth(medthick)) ///
xscale(r(-1.7 -0.2))  xlabel(none) yscale(r(0 1.25))  ylabel(0(0.2)1.2) xtitle("ln(productivity)", size(medlarge))  ytitle("Density", size(medlarge)) scheme(s1color) ///
text(0.05 `o' "cutoff for" "surviving", place(e) color(green) size(medium)) ///
text(0.05 `m' "cutoff for search" "vs. production", place(e) color(navy) size(medium)) ///
text(1 -1.60 "search for" "organizational" "knowledge", place(e) color(red) size(medlarge)) ///
text(1 -1.1 "produce", place(e) color(red) size(medlarge)) 
graph export "$output_appendix/Figures/Figure_A15B.eps", as(eps) replace
graph export "$output_appendix/Figures/Figure_A15B.pdf", as(pdf) replace



**Figure A.16: Resulting Productivity distribution after Innovation
local bandwidth = 0.2
sum ln_phi_t1 if produce==1
local m = r(min) //this is productivity of the lowest-prd firm that decides to produce in period 1
twoway (kdensity ln_phi_t1, bwidth(`bandwidth') lcolor(black) lpattern(dash) lwidth(thick)) (kdensity ln_phi_t3, bwidth(`bandwidth') lcolor(navy) lwidth(thick)), xline(`m', lcolor(navy) lwidth(medthick)) ///
xscale(r(-1.7 -0.2))  xlabel(none) yscale(r(0 1.25))  ylabel(0(0.2)1.2) xtitle("ln(productivity)", size(medlarge))  ytitle("Density", size(medlarge))  ///
legend(order(1 "Initial Prd Draws" 2 "Prd After Upgrading") size(medium) position(11) ring(0) rows(2)) scheme(s1color) ///
text(0.05 `m' "cutoff for search" "vs. production", place(e) color(navy) size(medium)) ///
text(0.3 -1.60 "search for" "organizational" "knowledge", place(e) color(red) size(medlarge)) ///
text(0.3 -1.1 "produce", place(e) color(red) size(medlarge)) 
graph export "$output_appendix/Figures/Figure_A16.eps", as(eps) replace
graph export "$output_appendix/Figures/Figure_A16.pdf", as(pdf) replace



**Paper Figure 3: Overall Productivity Dynamics
local bandwidth = 0.2
sum ln_phi_t1_all, d
local 5pctile = r(p5) //needed so we ignore the bottom 5% draws in the plot
sum ln_phi_t1_all if surviving_t1==1
local o = r(min) //this is productivity of the lowest-prd firm that decides to operate in period 1
sum ln_phi_t1 if produce==1
local m = r(min) //this is productivity of the lowest-prd firm that decides to produce in period 1
twoway (kdensity ln_phi_t1_all if ln_phi_t1_all>`5pctile', bwidth(`bandwidth') lcolor(black) lpattern(dash) lwidth(thick)) (kdensity ln_phi_t3, bwidth(`bandwidth') lcolor(navy) lwidth(thick)), xline(`m', lcolor(navy) lwidth(medthick)) ///
xline(`o', lcolor(green) lwidth(medthick))  xlabel(none) yscale(r(0 1.25))  ylabel(0(0.2)1.2) xtitle("ln(productivity)", size(medlarge))  ytitle("Density", size(medlarge))  ///
legend(order(1 "Initial Prd Draws" 2 "Final Prd Distribution") size(medium) position(11) ring(0) rows(2)) scheme(s1color) ///
text(0.05 `o' "cutoff for" "surviving", place(e) color(green) size(small)) ///
text(0.05 `m' "cutoff for search" "vs. production", place(e) color(navy) size(small)) ///
text(0.2 -2.6 "exit", place(e) color(red) size(medlarge)) ///
text(0.2 -1.6 "search", place(e) color(red) size(medlarge)) ///
text(0.2 -1.1 "produce", place(e) color(red) size(medlarge)) 
graph export "$output_main/Figures/Figure_3.eps", as(eps) replace
graph export "$output_main/Figures/Figure_3.pdf", as(pdf) replace







